define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'desk/index' + location.search,
                    add_url: 'desk/add',
                    edit_url: 'desk/edit',
                    del_url: 'desk/del',
                    multi_url: 'desk/multi',
                    import_url: 'desk/import',
                    table: 'desk',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                        checkbox: true
                    }, {
                        field: 'id',
                        title: __('Id')
                    }, {
                        field: 'name',
                        title: __('Name'),
                        operate: 'LIKE'
                    }, {
                        field: 'qrcode',
                        title: __('Qrcode'),
                        events: Table.api.events.image,
                        formatter: Table.api.formatter.image
                    }, {
                        field: 'operate',
                        title: __('Operate'),
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate
                    }, {
                        field: 'buttons',
                        title: __('生成二维码'),
                        table: table,
                        events: Table.api.events.operate,
                        buttons: [{
                            name: 'gencode',
                            text: __('生成二维码'),
                            title: __('生成二维码'),
                            classname: 'btn btn-xs btn-success btn-ajax',
                            icon: 'fa fa-list',
                            url: './desk/gencode',
                            success: function(data) {
                               Layer.alert("<textarea class='form-control' cols='60' rows='5'>" + data.result + "</textarea>", {
                                            title: __("执行结果"),
                                            shadeClose: true
                                        });
                                table.bootstrapTable('refresh');
                                return false;
                            },
                            visible: function(row) {
                                //返回true时按钮显示,返回false隐藏
                                if (row.qrcode) {
                                    return false;
                                }
                                return true;
                            }
                        }],
                        formatter: Table.api.formatter.buttons
                    }]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});